Imports System.IO

Public Class codeview
	Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

	'This call is required by the Web Form Designer.
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

	End Sub
	Protected WithEvents img As System.Web.UI.WebControls.Image
	Protected WithEvents preCode As System.Web.UI.HtmlControls.HtmlGenericControl

	'NOTE: The following placeholder declaration is required by the Web Form Designer.
	'Do not delete or move it.
	Private designerPlaceholderDeclaration As System.Object

	Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
		'CODEGEN: This method call is required by the Web Form Designer
		'Do not modify it using the code editor.
		InitializeComponent()
	End Sub

#End Region

	Private m_sImageUrl As String = ""
	Private m_sToolTip As String = ""
	Private m_sCodeFile As String = ""

	Public Property ToolTip() As String
		Get
			Return m_sToolTip
		End Get
		Set(ByVal Value As String)
			m_sToolTip = Value
		End Set
	End Property

	Public Property ImageUrl() As String
		Get
			Return m_sImageUrl
		End Get
		Set(ByVal Value As String)
			m_sImageUrl = Value
		End Set
	End Property

	Public Property CodeFile() As String
		Get
			Return m_sCodeFile
		End Get
		Set(ByVal Value As String)
			m_sCodeFile = Value
		End Set
	End Property

	Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Put user code to initialize the page here
		Dim aryImages() As String = ImageUrl.Split("`")		  'mini hack 
		Dim aryCodeFile() As String = CodeFile.Split("`")		  'mini hack 
		Dim aryToolTips() As String = ToolTip.Split("`")

		Dim oImg As System.Web.UI.WebControls.Image
		Dim oLiteral As LiteralControl
		Dim oCodePanel As Panel = New Panel
		For i As Integer = 0 To aryImages.Length - 1
			oImg = New System.Web.UI.WebControls.Image
			oImg.ID = "img" & i
			oImg.ImageUrl = ResolveUrl(aryImages(i))
			If aryToolTips.Length > i Then
				oImg.AlternateText = aryToolTips(i)
			End If
			oImg.Attributes.Add("onclick", "showHide(this)")
			Me.Controls.Add(oImg)

			oLiteral = New LiteralControl
			oLiteral.Text = String.Format("<pre id=""{0}"" class=""samplecode"" style=""display:none"">{1}</pre>", oImg.ClientID & "_code", GetFileContents(Request.MapPath(aryCodeFile(i))))
			oCodePanel.Controls.Add(oLiteral)
		Next
		Me.Controls.Add(oCodePanel)
	End Sub

	Public Shared Function GetFileContents(ByVal File As String) As String
		'when reading in text that contains character 0 the string stops since vb's strings are terminated by a null character
		Dim oFile As FileInfo = New FileInfo(File)
		Dim sr As StreamReader
		Dim s As String
		Dim sRet As String
		Try
			If oFile.Exists Then
				sr = oFile.OpenText
				While sr.Peek > -1
					s = sr.ReadLine()
					If Len(s) = 1 Then
						If Asc(s) <> 0 Then
							sRet &= s & vbCrLf
						End If
					Else
						sRet &= s & vbCrLf
					End If
				End While
			End If
			Return sRet
		Finally
			If Not sr Is Nothing Then sr.Close()
		End Try
	End Function


End Class
